<?php

  //Include database functions and connection
  include('inc/database.php');

  //Find ID from url
  $id = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], "/", 8) + 1);
  //Check ID is numeric
  if (!ctype_digit($id)) {
    die('Invalid ID');
  }

  //Grab the name and download from the database
  $sql  = 'SELECT addon_type, addon_title, version_download FROM versions NATURAL JOIN addons WHERE version_id = \''.m($id).'\'';
  $res  = mysql_query($sql) or die(mysql_error());

  //Check there's one result and grab the result
  if (mysql_num_rows($res) != 1) {
    die('Invalid ID');
  } else {
    $row = mysql_fetch_assoc($res);
  }

  //Check if the download is a download and send
  if ($row['version_download'] == '') {
    die('No Download');
  } else {
    header('Pragma: public');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Content-Type: application/zip');
    header('Content-Disposition: attachment; filename=' . str_replace(' ', '_', $row['addon_title']) . '.' . ($row['addon_type'] == 'plugin' ? 'jar' : 'zip'));
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: ' . strlen($row['version_download'])); 

    echo $row['version_download'];
  }
?>
